
// 16进制转rgb
export function hexToRgb(hexStr: string) {
  //十六进制颜色值的正则表达式
  let reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  let sColor = hexStr.toLowerCase();
  if (sColor && reg.test(sColor)) {
    if (sColor.length === 4) {
      let sColorNew = '#';
      for (let i = 1; i < 4; i += 1) {
        sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
      }
      sColor = sColorNew;
    }
    //处理六位的颜色值f
    let sColorChange: Array<number> = [];
    for (let i = 1; i < 7; i += 2) {
      sColorChange.push(parseInt(`0x${sColor.slice(i, i + 2)}`));
    }
    let rgbText = `rgb(${sColorChange.join(',')})`;
    return rgbText;
  } else {
    return sColor;
  }
}

// rgb转16进制
export function rgbToHex(color: string) {
  try {
    let start = color.indexOf('(');
    let end = color.indexOf(')');
    let list = color.substring(start + 1, end).split(',');
    let r = Number(list[0]),
      g = Number(list[1]),
      b = Number(list[2]);
    //十六进制颜色值的正则表达式
    return '#' + fAddZero(r.toString(16)) + fAddZero(g.toString(16)) + fAddZero(b.toString(16));
  } catch (error) {
    console.log(error);
    return color;
  }
}
function fAddZero(v) {
  let newv = '00' + v;
  return newv.substring(newv.length - 2, newv.length);
}
// rgba 透明度更改
export function rgbOpacity(rgbaText: string, opacity = 1) {
  opacity = Number(opacity.toFixed(2));
  let start = rgbaText.indexOf('(');
  let end = rgbaText.indexOf(')');
  let list = rgbaText.substring(start + 1, end).split(',');
  list[list.length - 1] = `${opacity}`;
  return `rgba(${list.join(',')})`;
}
/**
 *   颜色渐变
 *   num：颜色个数
 */
function fColorGradualChange(startColor: string, endColor: string, num: number) {
  let rgb = /^rgb|RGB\((([0-9]|[1-9]\d|1\d\d|2([0-4]\d|5[0-5])),){2}([0-9]|[1-9]\d|1\d\d|2([0-4]\d|5[0-5]))\)$/; //rgb
  let hex = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i; //16进制
  //颜色预处理
  let startRGB, endRGB;
  if (hex.test(startColor)) {
    startRGB = fAnalysisRGB(startColor);
  } else if (rgb.test(startColor)) {
    startRGB = startColor.substring(3, 15).split(',');
  }
  if (hex.test(endColor)) {
    endRGB = fAnalysisRGB(endColor);
  } else if (rgb.test(startColor)) {
    endRGB = endColor.substring(3, 15).split(',');
  }
  let startR = startRGB[0],
    startG = startRGB[1],
    startB = startRGB[2];
  let endR = endRGB[0],
    endG = endRGB[1],
    endB = endRGB[2];
  let sR = (endR - startR) / num;
  let sG = (endG - startG) / num;
  let sB = (endB - startB) / num;
  let colors: Array<string> = [];
  for (let i = 0; i < num; i++) {
    let rgb = `rgba(${parseInt(sR * i + startR)},${parseInt(sG * i + startG)},${parseInt(sB * i + startB)},1)`;
    colors.push(rgb);
  }
  return colors;
}
/**
 *   解析rgb格式
 */
function fAnalysisRGB(temp) {
  temp = temp.toLowerCase().substring(1, temp.length);
  let colors: Array<number> = [];
  colors.push(parseInt(temp.substring(0, 2), 16));
  colors.push(parseInt(temp.substring(2, 4), 16));
  colors.push(parseInt(temp.substring(4, 6), 16));
  return colors;
}
export function getColor(value: number) {
  value = value * 10;
  let step = 10;
  let colorZone = parseInt(`${value / step}`);
  let index = parseInt(`${value % step}`);
  let s: Array<string> = [];
  switch (colorZone) {
    case 0:
      s = fColorGradualChange('#FF0000', '#FF9A00', step);
      return s[index];
    case 1:
      s = fColorGradualChange('#FF9A00', '#CCFF00', step);
      return s[index];
    case 2:
      s = fColorGradualChange('#CCFF00', '#33FF00', step);
      return s[index];
    case 3:
      s = fColorGradualChange('#33FF00', '#00FF67', step);
      return s[index];
    case 4:
      s = fColorGradualChange('#00FF67', '#00FFFF', step);
      return s[index];
    case 5:
      s = fColorGradualChange('#00FFFF', '#0066FF', step);
      return s[index];
    case 6:
      s = fColorGradualChange('#0066FF', '#3300FF', step);
      return s[index];
    case 7:
      s = fColorGradualChange('#3300FF', '#CC00FF', step);
      return s[index];
    case 8:
      s = fColorGradualChange('#CC00FF', '#FF0099', step);
      return s[index];
    case 9:
      s = fColorGradualChange('#FF0099', '#FF0000', step);
      return s[index];
    case 10:
      return 'rgba(255,0,0,1)';
    default:
      return 'rgba(255,0,0,1)';
  }
}
//
// #ffffff - #ff0000
// #ffffff - #000000
// #ff0000 - #000000
// 色域规则模板
export function colorCursor(color: string, x: number, y: number) {
  color = rgbToHex(color);
  x = Number(x.toFixed(0));
  y = Number(y.toFixed(0));
  let step = 100;
  let result1: string = '';
  let result2: string = '';

  // x白色无法取极致
  if (x === 100) result1 = 'rgba(255,255,255,1)';
  else result1 = fColorGradualChange(color, '#ffffff', step)[x];
  // y黑色无法取极致 y=100时为黑色直接返回
  if (y === 100) return 'rgba(0,0,0,1)';
  else result2 = fColorGradualChange(color, '#000000', step)[y];
  // 综合结果
  return fColorGradualChange(rgbToHex(result1), rgbToHex(result2), step)[y];
}
